package com.estate.common.utils;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.estate.common.core.page.PageDomain;
import com.estate.common.core.page.TableSupport;

import java.util.Objects;

/**
 * 分页工具类
 *
 * @author ruoyi
 */
public class PageUtils {

    private static final ThreadLocal<PageDomain> PAGE_LOCAL = new ThreadLocal<>();

    public static void startPage() {
        PAGE_LOCAL.set(TableSupport.buildPageRequest());
    }

    public static <T> Page<T> getPage(T data) {
        PageDomain pageDomain = PAGE_LOCAL.get();
        Page<T> page;
        if (Objects.isNull(pageDomain) || Objects.isNull(pageDomain.getPageSize()) || pageDomain.getPageSize() <= 0) {
            page = null;
        } else {
            page = new Page<>(pageDomain.getPageNum(), pageDomain.getPageSize());
        }
        return page;
    }

    public static void clearPage() {
        PAGE_LOCAL.remove();
    }

}
